<%--
  Created by IntelliJ IDEA.
  User: 15890741124
  Date: 2022/8/9
  Time: 14:36
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ include file="../common.jsp"%>
<html>
<head>
    <title>详细信息</title>
</head>
<body>
<%--查询--%>
<form class="layui-form" action="" style="margin-top: 20px">
    <div class="layui-inline">
        <label class="layui-form-label">用户名</label>
        <div class="layui-input-block">
            <input type="text" name="username" id="username" placeholder="请输入用户名" autocomplete="off"
                   class="layui-input">
        </div>
    </div>
    <div class="layui-inline">
        <label class="layui-form-label">部门编号</label>
        <div class="layui-input-inline">
            <input type="text" name="deptid" id="deptid" placeholder="请输入部门编号" autocomplete="off" class="layui-input">
        </div>
    </div>

    <div class="layui-inline">
        <div class="layui-input-block">
            <button class="layui-btn" lay-submit lay-filter="formDemo">立即提交</button>
            <button class="layui-btn layui-btn-primary" id="reset" type="reset">重置</button>
        </div>
    </div>
</form>
<%--表格--%>
<table id="demo" lay-filter="test"></table>
<%--表格右侧标签--%>
<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="edit">修改</a>
    <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="resetpassword">重置密码</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除 </a>
</script>
<%--表格头标签--%>
<script type="text/html" id="toolbarDemo">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-sm" lay-event="add">添加</button>
        <button class="layui-btn layui-btn-danger layui-btn-sm" lay-event="del">删除</button>
    </div>
</script>
<%-- 添加表单  --%>
<form class="layui-form" action="" id="addForm" style="display: none" enctype="multipart/form-data">
    <div class="layui-form-item">
        <label class="layui-form-label">用户名</label>
        <div class="layui-input-block">
            <%--onblur="checkUsername(obj)"--%>
            <input type="text" name="username" id="user" lay-verify="required|username" placeholder="请输入用户名"
                   autocomplete="off" class="layui-input">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">密码</label>
        <div class="layui-input-block">
            <input type="text" name="password" lay-verify="required|password" placeholder="请输入密码" autocomplete="off"
                   class="layui-input">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">部门</label>
        <div class="layui-input-block">
            <select name="deptid" lay-filter="dept" id="deptidSelect">
                <%--                    js填充部门的下拉选项--%>
            </select>
        </div>
    </div>

    <div class="layui-form-item">
        <%--   需添加邮箱格式验证     --%>
        <label class="layui-form-label">邮箱</label>
        <div class="layui-input-block">
            <input type="text" name="email" lay-verify="required|email" placeholder="请输入邮箱" autocomplete="off"
                   class="layui-input">
        </div>
    </div>

    <div class="layui-form-item">
        <%--   需添加手机号长度验证     --%>
        <label class="layui-form-label">手机号</label>
        <div class="layui-input-block">
            <input type="text" name="phonenumber" lay-verify="required|phone" placeholder="请输入手机号" autocomplete="off"
                   class="layui-input">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">性别:</label>
        <div class="layui-input-block">
            <label for="man">
                🚹男
            </label>
            <input type="radio" name="sex" id="man" class="layui-input" value="1" checked/>
            <label for="woman">
                🚺女
            </label>
            <input type="radio" name="sex" id="woman" value="0" class="layui-input"/>
        </div>
    </div>

    <div class="layui-form-item" style="display: none">
        <%--   需添加单选框 状态    --%>
        <div class="layui-input-block">
            <input type="hidden" name="status" class="layui-input" value="1" checked/>

        </div>
    </div>

    <%--   <div class="layui-form-item">
           &lt;%&ndash;   需添加单选框     &ndash;%&gt;
           <label class="layui-form-label">性别</label>
           <div class="layui-input-block">
               <input type="text" name="sex" lay-verify="required" placeholder="" autocomplete="off" class="layui-input">
           </div>
       </div>--%>

    <div class="layui-form-item">
        <input type="hidden" name="avatar" id="avatar">
        <%--        <label class="layui-form-label">头像</label>--%>
        <div class="layui-upload">
            <div class="layui-upload-list">
                <img class="layui-upload-img" id="demo1" src="" style="width: 100px;height: 100px">
                <p id="demoText"></p>
            </div>
        </div>
        <button type="button" class="layui-btn" id="addimg">
            上传图片
        </button>
    </div>


    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn" lay-submit lay-filter="addUserFilter">立即提交</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
    </div>
</form>
<%-- 修改表单 根据姓名修改自己的部门 --%>
<form class="layui-form" action="" lay-filter="updateFromFilter" id="updateForm" style="display: none">
    <div class="layui-form-item">
        <label class="layui-form-label">用户名</label>
        <div class="layui-input-block">
            <input type="text" name="username" id="name" readonly placeholder="请输入用户名" autocomplete="off"
                   class="layui-input"><span style="color: red"></span>
        </div>
    </div>
    <%-- <div class="layui-form-item">
         <label class="layui-form-label">密码</label>
         <div class="layui-input-block">
             <input type="text" name="password" lay-verify="required" placeholder="请输入密码" autocomplete="off" class="layui-input">
         </div>
     </div>--%>
    <div class="layui-form-item">
        <label class="layui-form-label">部门</label>
        <div class="layui-input-block">
            <select name="deptid" lay-filter="updatedept" id="updateSelect">
                <%--                    js填充部门的下拉选项--%>
            </select>
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn" lay-submit lay-filter="updateUserFilter" id="updateUserFilter">立即提交</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
    </div>
</form>
<%-- 重置密码表单 --%>
<form class="layui-form" action="" lay-filter="passwordFilter" id="updatePassword" style="display: none">
    <%--用来接收id值--%>
    <div style="height: 0">
        <div>
            <input type="text" name="userid" id="userid" lay-verify="required" style="display: none" placeholder="请输入密码"
                   autocomplete="off" class="layui-input">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">密码</label>
        <div class="layui-input-block">
            <input type="text" name="password" id="pass" placeholder="请输入密码" autocomplete="off" class="layui-input">
        </div>
    </div>

    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn" lay-submit lay-filter="updatePasswordFilter">立即提交</button>
            <%--<button type="reset" class="layui-btn layui-btn-primary">重置</button>--%>
        </div>
    </div>
</form>

</body>
<script>


    layui.use(['table', 'form', 'upload'], function () {
        var table = layui.table;
        var form = layui.form;
        var upload = layui.upload;
        table.render({
            elem: '#demo'
            , height: 550
            , url: '${pageContext.request.contextPath}/user/query.do'
            , dataType: 'json'
            , page: true //开启分页
            // ,toolbar:'default'
            , limits: [5, 10, 15, 20, 25, 30, 100, 200]//将每页几个写在这里，不想默认10个每页
            , autoSort: true//关闭自动排序
            , cols: [[
                {checkbox: 'checkbox', fixed: 'left'}//左侧复选框
                , {field: 'userid', title: 'ID', width: 80, sort: true, fixed: 'left'}
                , {field: 'username', title: '账号名', width: 120}
                , {field: 'password', title: '密码', width: 80, sort: true}
                , {field: 'salt', title: '盐值', width: 200, sort: true,hide: true}
                , {field: 'email', title: '邮箱', width: 200, sort: true}
                , {field: 'phonenumber', title: '手机号', width: 200, sort: true}
                , {
                    field: 'sex', title: '性别', width: 200,
                    templet: function (d) {
                        if(d.sex==1){
                            return '<span >男</span>'
                        }
                        return '<span >女</span>'
                    }
                }
                , {field: 'deptid', title: '部门编号', width: 200, sort: true, hide: true}
                , {field: 'deptName', title: '部门名称', width: 200, sort: true}
                , {field: 'roleName', title: '角色', width: 200, sort: true}
                , {field: 'status', title: '状态', width: 200,

                    templet: function (d) {
                        if(d.status==1){
                            return '<span  class="layui-btn layui-btn-xs" lay-event="state">可用</span>'
                        }
                        return '<span class="layui-btn layui-btn-xs layui-btn-danger" lay-event="state">禁用</span>'
                    }
                }
                , {field: 'delFlag', title: '删除标记', width: 200, sort: true, hide: true}
                , {
                    field: 'avatar', title: '头像', width: 200
                    , templet: function (d) {
                        return '<img src=' + d.avatar + '>'
                    }
                }
                , {field: 'createBy', title: '创建人', width: 200, sort: true}
                , {field: 'createTime', title: '创建时间', width: 200, sort: true}
                , {field: 'updateBy', title: '修改人', width: 200, sort: true}
                , {field: 'updateTime', title: '修改时间', width: 200, sort: true}
                , {field: 'remark', title: '备注', width: 200, sort: true}
                , {field: 'roleId', title: '角色id', width: 200, sort: true, hide: true}
                //右侧操作栏
                , {fixed: 'right', title: '操作', width: 200, align: 'center', toolbar: '#barDemo'}
            ]]
            , toolbar: "#toolbarDemo"//头部操作栏
        });



        //触发头部工具栏
        table.on('toolbar(test)', function (obj) {
            var checkStatus = table.checkStatus(obj.config.id)
                , data = checkStatus.data; //获取选中的数据

            switch (obj.event) {
                case "del":
                    if (data.length === 0) {
                        layer.msg('请选择一行');
                    } else {
                        //定义一个数组，封装所有的id
                        var ids = [];
                        //循环所有选中的行
                        for (let i = 0; i < data.length; i++) {
                            ids[i] = data[i].userid;
                        }
                        layer.confirm('真的删除行么', function (index) {
                            $.ajax({
                                url: "${pageContext.request.contextPath}/user/delete.do",
                                data: {userid: ids.join(",")},
                                //响应的数据类型
                                dataType: "JSON",
                                type: "POST"
                                //异步请求
                                // async: true,
                                //成功的回调
                                , success: function (data) {
                                    if (data.code == 0) {
                                        layer.closeAll();//关闭所有
                                        table.reload('demo');//刷新表格
                                    }
                                    layer.msg(data.msg);
                                }
                            });

                        });
                    }
                    break;
                case "add":
                    $.ajax({
                        url: "${pageContext.request.contextPath}/dept/query.do",
                        type: "post",
                        dataType: "json",
                        success: function (data) {
                            //将dept表中的数据添加到下拉列表中去
                            if (data.code == 0) {
                                var htmlStr = "";
                                for (let i = 0; i < data.data.length; i++) {
                                    htmlStr += "<option value = " + data.data[i].deptId + ">" + data.data[i].deptName + "<option>"
                                }
                                $("#deptidSelect").html(htmlStr);
                                layui.use('form', function () {
                                    var form = layui.form;
                                    form.render('select');
                                });
                            }
                        }
                    });
                    layer.open({//弹出添加框
                        title:'添加',
                        type: 1,
                        content: $("#addForm"),
                        area: ('500px', '600px')
                    });
                    break;
                default:
                    break;
            }
        });

        //提交添加页面
        form.on('submit(addUserFilter)', function (data) {
            $.ajax({
                url: "${pageContext.request.contextPath}/user/insert.do"
                , data: data.field
                , dataType: "json"
                , type: "POST"
                , success: function (dat) {
                    if (dat.code == 0) {
                        layer.closeAll();
                        table.reload('demo', {
                            page: {
                                curr: 1
                                //重新从第 1 页开始
                            }
                        });
                    }
                    layer.msg(dat.msg);
                }
            });
            return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
        });

        //监听右侧工具栏事件
        table.on('tool(test)', function (obj) {
            switch (obj.event) {
                case "del":
                    layer.confirm('真的删除行么', function (index) {
                        $.ajax({
                            url: "${pageContext.request.contextPath}/user/delete.do",
                            data: {"userid": obj.data.userid},
                            //响应的数据类型
                            dataType: "JSON",
                            type: "POST"
                            //异步请求
                            // async: true,
                            //成功的回调
                            , success: function (data) {
                                if (data.code == 0) {
                                    layer.closeAll();
                                    table.reload('demo');
                                }
                                layer.msg(data.msg);
                            }
                        });
                    });
                    break;
                case "edit":
                    var name = obj.data.username;
                    //将选择的admin填入到表单中
                    form.val("updateFromFilter", { //formTest 即 class="layui-form" 所在元素属性 lay-filter="" 对应的值
                        "username": name // "name": "value"
                    });
                    $.ajax({
                        url: "${pageContext.request.contextPath}/dept/query.do",
                        type: "post",
                        dataType: "json",
                        success: function (data) {
                            //将dept表中的数据添加到下拉列表中去
                            if (data.code == 0) {
                                var htmlStr = "";
                                for (let i = 0; i < data.data.length; i++) {
                                    htmlStr += "<option value = " + data.data[i].deptId + ">" + data.data[i].deptName + "<option>"
                                }
                                $("#updateSelect").html(htmlStr);
                                layui.use('form', function () {
                                    var form = layui.form;
                                    form.render('select');
                                });
                            }
                        }
                    });
                    layer.open({//弹出修改框
                        title:'修改',
                        type: 1,
                        content: $("#updateForm"),
                        area: ('500px', '600px')
                    });
                    break;
                case "resetpassword":
                    var userid = obj.data.userid;
                    form.val("passwordFilter", {
                        "userid": userid
                    });
                    layer.open({//弹出修改框
                        title:'修改密码',
                        type: 1,
                        content: $("#updatePassword"),
                        area: ('500px', '600px')
                    });
                    break;
                default:
                    break;
            }
        });

        //重置密码
        form.on('submit(updatePasswordFilter)', function (data) {
            $.ajax({
                url: "${pageContext.request.contextPath}/user/updatePassword.do"
                , data: data.field
                , dataType: "json"
                , type: "POST"
                , success: function (dat) {
                    if (dat.code == 0) {
                        layer.closeAll();
                        table.reload('demo', {
                            page: {
                                curr: 1
                                //重新从第 1 页开始
                            }
                        });
                    }
                    layer.msg(dat.msg);
                }
            });
            return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
        });

        //提交修改页面
        form.on('submit(updateUserFilter)', function (data) {
            $.ajax({
                url: "${pageContext.request.contextPath}/user/update.do"
                , data: data.field
                , dataType: "json"
                , type: "POST"
                , success: function (data) {
                    if (data.code == 0) {
                        layer.closeAll();
                        table.reload('demo', {
                            page: {
                                curr: 1
                                //重新从第 1 页开始
                            }
                        });
                    }
                    layer.msg(data.msg);
                }
            });
            return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
        });
        /*
       * 修改用户名唯一性校验
       * */

        $("#name").blur(function (){
            $.ajax({
                url: "${pageContext.request.contextPath}/user/queryUserByName.do",
                data: {username: $("#name").val(), id: $("#id").val()},
                dataType: "json",
                type: "POST",
                success: function (re) {
                    if (re.code == 0) {
                        $("#name").next().html(re.msg);
                        $('#updateUserFilter').removeClass("layui-btn-disabled").attr("disabled", false);
                    } else {
                        $("#name").next().html(re.msg);
                        $('#updateUserFilter').addClass("layui-btn-disabled").attr("disabled", true);
                    }
                }
            })
        });

        //查询问题
        form.on('submit(formDemo)', function (data) {
            table.reload('demo', {
                where: {
                    //请求参数（注意：这里面的参数可任意定义，并非下面固定的格式）
                    username: data.field.username //排序字段-->查询姓名
                    , deptid: data.field.deptid//排序方式 --> 查询部门编号
                }
                , page: {
                    curr: 1
                    //重新从第 1 页开始
                },
            });
            return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
        });


        //重置查询条件
        $("#reset").click(function () {
            table.reload('demo', {
                where: { //请求参数（注意：这里面的参数可任意定义，并非下面固定的格式）
                    username: "" //第一个查询条件
                    , deptid: ""//第二个查询条件
                },
                page: {
                    curr: 1
                    //重新从第 1 页开始
                },
            });
        });


        //对username绑定失去焦点事件，发送ajax请求
        $("#user").blur(function () {
            var username = $("#user").val();
            $.ajax({
                url: "${pageContext.request.contextPath}/user/queryUserByName.do",
                type: "POST",
                dataType: "JSON",
                data: {username: username},
                async: false,
                success: function (data) {
                    if (data.code == 0) {
                        layer.msg(data.msg);
                        $("#user").val("")
                    }
                }
            });
            /* table.reload('demo', {
                 where: { //请求参数（注意：这里面的参数可任意定义，并非下面固定的格式）
                     username: "" //第一个查询条件
                     , deptid: ""//第二个查询条件
                 },
                 page: {
                     curr: 1
                     //重新从第 1 页开始
                 },
             });*/
        })

        //提交图片按钮发送ajax请求
        var uploadInst = upload.render({
            elem: '#addimg'
            , url: '${pageContext.request.contextPath}/upload.do'
            , done: function (res) {
                if (res.code == 0) {
                    let src = res.msg;
                    $("#avatar").val(src);
                    $("#demo1").attr('src', src);
                } else {
                    return layer.msg("上传失败")
                }
            }
            , error: function () {

            }
        });
        form.verify({
            //表单验证手机号
            phone: function (value, item) { //value：表单的值、item：表单的DOM对象

                if (!new RegExp("^1([358][0-9]|4[579]|66|7[0135678]|9[89])[0-9]{8}$").test(value)) {
                    return '手机号不合法';
                }

            },
           /* email: function (value, item) { //value：表单的值、item：表单的DOM对象

              /!*if (!new RegExp("/^[A-Za-z0-9\-_]+[A-Za-z0-9\.\-_]*[A-Za-z0-9\-_]+@[A-Za-z0-9]+[A-Za-z0-9\.\-_]*(\.[A-Za-z0-9\.\-_]+)*[A-Za-z0-9]+\.[A-Za-z0-9]+[A-Za-z0-9\.\-_]*[A-Za-z0-9]+$/").test(value)){
                   return '邮箱必须包含@和.';
                }
*!/
            }*/
        });


    });

</script>
</html>
